package org.jsrml.sv.mob.controller.purchaser;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.jsrml.common.pagination.Pagination;
import org.jsrml.common.util.JSONResultDTO;
import org.jsrml.core.purchaser.command.RemovePositionsCo;
import org.jsrml.core.purchaser.command.SavePositionsCo;
import org.jsrml.core.purchaser.entity.Positions;
import org.jsrml.core.purchaser.exception.PurchaserException;
import org.jsrml.core.purchaser.qo.PositionsQO;
import org.jsrml.core.purchaser.service.PositionsService;
import org.jsrml.sv.mob.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/positions")
public class PositionsController extends BaseController {
	
	@Autowired
	private PositionsService positionsService;
	
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	@ResponseBody
	public JSONResultDTO create(@RequestBody SavePositionsCo command) {
		Positions positions = positionsService.createPositions(command);
		return createSuccessResult(positions);
	}

	@RequestMapping(value = "/modify", method = RequestMethod.POST)
	@ResponseBody
	public JSONResultDTO modify(@RequestBody SavePositionsCo command) {
		Positions positions = null;
		try {
			positions = positionsService.modifyPositions(command);
		} catch (PurchaserException e) {
			return createFailResult("-1", e.getMessage(), null);
		} catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		return createSuccessResult(positions);
	}

	@RequestMapping(value = "/remove", method = RequestMethod.POST)
	@ResponseBody
	public JSONResultDTO remove(@RequestBody RemovePositionsCo command) {
		
		try {
			positionsService.removePositions(command);
		} catch (PurchaserException e) {
			return createFailResult("-1", e.getMessage(), null);
		} catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		return createSuccessResult(null);
	}
	
	@RequestMapping(value = "/pl/remove", method = RequestMethod.POST)
	@ResponseBody
	public JSONResultDTO plRemove(@RequestBody RemovePositionsCo command) {
		try {
			positionsService.plRemovePositions(command);
		} catch (PurchaserException e) {
			return createFailResult("-1", e.getMessage(), null);
		} catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		return createSuccessResult(null);
	}
	
	@RequestMapping("/query")
	@ResponseBody
	public JSONResultDTO query(HttpServletRequest request, @RequestBody PositionsQO qo) {
		qo.setRemove(false);
		try {
			switch (qo.getResultType()) {
			case 0:
				Integer count = positionsService.queryCount(qo);
				return createSuccessResult(count);
			case 1:
				Positions positions = positionsService.queryUnique(qo);
				return createSuccessResult(positions);
			case 2:
				List<Positions> list = positionsService.queryList(qo);
				return createSuccessResult(list);
			case 3:
				Pagination pagination = positionsService.queryPagination(qo);
				return createSuccessResult(pagination);
			default:
				return createFailResult("-1", "参数中缺少正确的resultType", null);
			}
		} catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
	}

	
}
